import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class Test1 {
    //Stirng和StringBuilder，StringBuffer相互转换
    public static void main(String[] args) {
        String str = "abc";
        StringBuilder sb = new StringBuilder(str);
        System.out.println(sb);
        sb.append(str);
        System.out.println(sb);
        StringBuilder sb1 = new StringBuilder();
        sb1.append("qwe");
        str = sb1.toString();
        System.out.println(str);


    }



    public static void main5(String[] args) {
        String str = "abc";
        StringBuilder sb = new StringBuilder();
        sb.append(str);
        for (int i = 0; i < 10; i++) {
            sb.append(i);
        }
        System.out.println(sb);
    }



    //关于字符串操作的一些函数
    public static void main4(String[] args) {
        int[] arr1 = {1, 2, 3};
        int[] arr2 = {1, 2, 3};
        System.out.println(arr1.equals(arr2));
        String str1 = "123";
        String str2 = "123";
        System.out.println(str1.equals(str2));
        String str3 = "abcdefa";
        String str4 = "Abc";
        //忽略大小写
        System.out.println(str3.equalsIgnoreCase(str4));
        //查找字符串是否存在只串
        System.out.println(str3.contains("b"));
        System.out.println(str3.indexOf("a",2));
        str3.lastIndexOf("a");
        str3.lastIndexOf("a",2);
        System.out.println(str3.startsWith("a"));
        System.out.println(str3.startsWith("a",5));
        System.out.println(str3.endsWith("a"));
        System.out.println(str3.endsWith("a"));
        //不是更改原来的字符串，而是从新生成了一个对象
        System.out.println(str3.replace('a', 'b'));
        System.out.println(str3.replaceAll("abc", "b"));
        System.out.println(str3.replaceFirst("a", "b"));
        String str5 = "1&2&3&4";
        System.out.println(Arrays.toString(str5.split("&")));
        String str6 = "153.34.43.44";
        //如果分割符是"\"，需要用"////"表示，特殊字符都要转义
        System.out.println(Arrays.toString(str6.split("\\.")));
        System.out.println(Arrays.toString(str6.split("\\.",2)));
        System.out.println(str6.substring(2));
        System.out.println(str6.substring(2,5));
        //去除字符串左右两端的空格
        String str7 = "  abc  DEF   ";
        System.out.println(str7.trim());
        System.out.println(str7.toUpperCase());
        System.out.println(str7.toLowerCase());
        //字符串入池
        str1.intern();
        //字符串拼接，拼接后的字符串不入常量池
        System.out.println(str7.concat("g"));
        //字符串是否为空
        System.out.println(str7.isEmpty());
        StringBuffer buffer = new StringBuffer();
        buffer.append("hello");
        System.out.println(buffer);
        buffer.append("and");
        System.out.println(buffer);

    }
        //字符串和字节想互转换
    public static void main3(String[] args) throws UnsupportedEncodingException {
        byte[] bytes = {97, 98, 99, 100};
        String str = new String(bytes);
        System.out.println(str);
        String str2 = new String(bytes,1,3);
        System.out.println(str2);
        String str3 = "abcd";
        byte[] bytes1 = str3.getBytes();
        System.out.println(Arrays.toString(bytes1));
        String str4 = "哈哈";
        byte[] bytes2 = str4.getBytes("GBK");
        System.out.println(Arrays.toString(bytes2));
    }





    //字符串和字符之间相互比较
    public static boolean isNum(String str){
     for (int i = 0; i < str.length(); i++) {
            char x = str.charAt(i);
            Boolean boo = Character.isDigit(x);   //学习Character类
            if(boo==false){
                return false;
            }
        }
        char[] arr1 = str.toCharArray();
        for (int i = 0; i < arr1.length; i++) {
            char arr2 = arr1[i];
            if(arr2 < '0' || arr2 > '9'){
                return false;
            }
        }

        return true;
    }

    public static void main2(String[] args) {
        char[] arr = {'a','b','c','d'};
        String str1 = new String(arr);
        System.out.println(str1);
        String str2 = new String(arr,1,3);
        System.out.println(str2);
        String str3 = "abcde";
        char arr1 = str3.charAt(2);
        System.out.println(arr1);
        char[] arr2 = str3.toCharArray();
        System.out.println(Arrays.toString(arr2));
        String str4 = "12345";
        System.out.println(isNum(str4));

    }






    //字符串底部存储
    public static void main1(String[] args) {
        int[] arr1 = {1,2,3};
        int[] arr2 = {1,2,3};
        String A = new String("123");
        String B = new String("123");
        String s1 = "abc";
        String s2 = "abc";
        String s3 = new String("abc");
        System.out.println(arr1==arr2);
        System.out.println(s1==s2);
        System.out.println(s3==s2);
        System.out.println(A.equals(B));
    }
}
